package in.muton.edushare.pages.curricula;

import in.muton.edushare.EduShareSession;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.edushare.common.dao.CoursesDAO;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.Courses;

//Class to pack subject
public class CourseListPacker {
	private static Log log = LogFactory.getLog(CourseListPacker.class);  
	private List<Courses> mandatoryCourseList = new ArrayList<Courses>();
	private List<List<Courses>> optionalCourseList =  new ArrayList<List<Courses>>();
	private String courseString = "";
	
	public CourseListPacker(List<Courses> mandatoryCourseList){
		this.mandatoryCourseList = mandatoryCourseList;		
	}
	
	public void addOptionalCourses(List<Courses> optionalCourseList){
		this.optionalCourseList.add(optionalCourseList);
	}
	
	public String getCourseString(){
		courseString = "man:";
		for(Courses c:mandatoryCourseList){
			courseString += (c.getCourseId() + ",");
		}
		courseString = courseString.substring(0, courseString.length()-1);
		courseString+=";";		
		for(int i=0;i< optionalCourseList.size();i++){
			courseString += "opt"+i+":";
			for(Courses c:optionalCourseList.get(i)){
				courseString += (c.getCourseId() + ",");
			}
			courseString = courseString.substring(0, courseString.length()-1);
			courseString+=";";
		}		
		return courseString;
	}
	
	public CourseListPacker(String courseListString){		
		log.debug("Input from user" + courseListString);
		
		this.courseString = courseListString;
		String[] tokens = courseString.split(";");
		mandatoryCourseList.clear();
		optionalCourseList.clear();		
		CoursesDAO cDAO = new CoursesDAO(EduShareSession.get().getSessionORMInterface());
		String[] firstToken = tokens[0].split(":"); 
		if(!(firstToken.length == 1 || firstToken[1].length() == 0)){
			String [] courseIds = firstToken[1].split(",");
			for(int i=0;i<courseIds.length;i++){
				log.debug("Found course id = " + courseIds[i]);
				if(StringUtil.isValid(courseIds[i])){
					mandatoryCourseList.add(
						cDAO.getCourseById(Integer.parseInt(courseIds[i])));
				}
			}
		}
		
		log.debug("Parsing optional courses");
		
		int optionalSlots = 0; 
		for(int i=1;i<tokens.length;i++){
			 if(tokens[i].length()>0){
				 optionalSlots ++;
				 String[] firstToken1 = tokens[i].split(":");
				 if(!(firstToken1.length == 1 || firstToken1[1].length()==0)){
					log.debug("Found course id = " + firstToken1[0]);
					List<Courses> courseList = new ArrayList<Courses>();
					String[] optTokens = firstToken1[1].split(",");
					for(int j=0;j<optTokens.length;j++){
						log.debug("Found course id = " + optTokens[j]);
						Courses c = cDAO.getCourseById(Integer.parseInt(optTokens[j]));
						if(!courseList.contains(c))
							courseList.add(cDAO.getCourseById(Integer.parseInt(optTokens[j])));
					}
					optionalCourseList.add(courseList);
				 }
			 }
		}
		
		log.debug("Parsed all courses");
	}
	
	public List<Courses> getMandatoryCourses(){
		return mandatoryCourseList;
	}
	
	public int getNumOptionalCourses(){
		return optionalCourseList.size();
	}
	
	public List<Courses> getOptionalCourses(int i){
		return optionalCourseList.get(i);
	}
}
